﻿// ------------------------------------------------------------------------------------------------
//  <copyright file="LocalFile.cs" company="Iveely">
//    Copyright (c) Iveely Liu.  All rights reserved.
//  </copyright>
//  
//  <Create Time>
//    03/02/2013 21:59 
//  </Create Time>
//  
//  <contact owner>
//    liufanping@iveely.com 
//  </contact owner>
//  -----------------------------------------------------------------------------------------------

#region

using System;
using System.Collections.Generic;
using System.IO;
using IveelySE.Common;
using IveelySE.Config;

#endregion

namespace IveelySE.IDFS.Store
{
    /// <summary>
    ///   本地文件操作
    ///   （如果是linux 则重写此文件，此时默认全是Windows文件操作）
    /// </summary>
    public class LocalFile
    {
        /// <summary>
        ///   异步对象
        /// </summary>
        private static readonly object sync = new object();

        /// <summary>
        ///   创建本地文件
        /// </summary>
        /// <param name="fileName"> 文件名 </param>
        public static void CreateFile(string fileName)
        {
            //调用windows文件创建
            File.AppendAllText(Application.GetCrawlerTempDir() + fileName, "");
        }

        /// <summary>
        ///   以行为方式向文件追加数据
        /// </summary>
        /// <param name="fileName"> 文件名 </param>
        /// <param name="data"> 文本数组 </param>
        public static void AppendText(string fileName, PObject data)
        {
            var list = (List<PObject>) Serialize.UnSerailzable(Application.GetCrawlerTempDir() + fileName) ??
                       new List<PObject>();
            list.Add(data);
            Serialize.Serailzable(list, Application.GetCrawlerTempDir() + fileName);
        }

        /// <summary>
        ///   获取对象
        /// </summary>
        /// <param name="fileName"> 序列化文件 </param>
        /// <returns> 返回对象 </returns>
        public static Object GetObj(string fileName)
        {
            //如果序列文件存在
            if(File.Exists(Application.GetCrawlerTempDir() + fileName))
            {
                //执行反序列化
                return Serialize.UnSerailzable(Application.GetCrawlerTempDir() + fileName);
            }
            //返回空
            return null;
        }

        /// <summary>
        ///   获取对象(当搜索的时候调用)
        /// </summary>
        /// <param name="fileName"> 序列化文件 </param>
        /// <returns> 返回对象 </returns>
        public static Object GetObjWhenSearch(string fileName)
        {
            //如果序列文件存在
            if(File.Exists(Application.GetDataDir() + fileName))
            {
                //执行反序列化
                return Serialize.UnSerailzable(Application.GetDataDir() + fileName);
            }
            //返回空
            return null;
        }

        /// <summary>
        ///   设定序列化文件
        /// </summary>
        /// <param name="fileName"> 文件名 </param>
        /// <param name="obj"> 对象 </param>
        public static bool SetObj(string fileName, Object obj)
        {
            lock(sync)
            {
                return Serialize.Serailzable(obj, Application.GetCrawlerTempDir() + fileName);
            }
        }
    }
}